using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.Linq;
using System.Linq;


using Soussous.Business;
using Soussous.DAO;
using Soussous.DAO.Connection;

namespace Soussous.DAO.Impl
{
    class NoteDAO : AbstractDAO<Note>
    {
        private static NoteDAO INSTANCE = new NoteDAO();

        public static NoteDAO getInstance()
        {
            return INSTANCE;
        }

        private NoteDAO()
        {
        }
		
		public Table<Note> getTable(){
			
			return NoteDAO.getInstance().getAll();
		
		}
        
        public List<Note> getAllNote() 
        {


            var NoteList = from e in getTable()
                           select e;
            
            return NoteList.ToList();
        }
		
		public Note searchNoteById(int id)
		{

            var NoteList = from e in getTable()
                           where e.Id == id
                           select e;
			
			
			return NoteList.Count() > 0 ? NoteList.First() : null;
		}
		
		public void updateNoteById(int id,Note note) {

            Table<Note> notes = getTable();
            var update = notes.First(e => e.Id == id);
          
            update.Date = note.Date;
            update.Description = note.Description;
            notes.Context.SubmitChanges();
		}

        public void deleteNoteById(int id)
        {
            Table<Note> notes = getTable();
            var result = (from note in notes
                          where note.Id == id
                          select note);
            notes.DeleteOnSubmit(result.Single());
            notes.Context.SubmitChanges();

        }
       
    }
}